package com.zycfc.zsf.boot.util.ead;

import java.security.*;
import javax.crypto.spec.*;
import javax.crypto.*;

public class AESUtils
{
    private static final String KEY_ALGORITHM = "AES";
    private static final String DEFAULT_CIPHER_ALGORITHM = "AES/ECB/PKCS5Padding";
    private static final String UTF_8 = "UTF-8";
    
    public static byte[] initSecretKey() {
        KeyGenerator kg = null;
        try {
            kg = KeyGenerator.getInstance("AES");
        }
        catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
            return new byte[0];
        }
        kg.init(128);
        final SecretKey secretKey = kg.generateKey();
        return secretKey.getEncoded();
    }
    
    public static Key toKey(final byte[] key) {
        return new SecretKeySpec(key, "AES");
    }
    
    public static String encodeHex(final String data, final Key key) throws Exception {
        final byte[] encryptData = encrypt(data, key);
        return Hex.encodeHexStr(encryptData);
    }
    
    public static String decodeHex(final String hexStr, final Key key) throws Exception {
        final byte[] b = decrypt(Hex.decodeHex(hexStr.toCharArray()), key);
        return new String(b);
    }
    
    public static byte[] encrypt(final String data, final String key) throws Exception {
        return encrypt(data.getBytes("UTF-8"), key.getBytes("UTF-8"), "AES/ECB/PKCS5Padding");
    }
    
    public static byte[] encrypt(final String data, final Key key) throws Exception {
        return encrypt(data.getBytes("UTF-8"), key, "AES/ECB/PKCS5Padding");
    }
    
    public static byte[] encrypt(final byte[] data, final Key key) throws Exception {
        return encrypt(data, key, "AES/ECB/PKCS5Padding");
    }
    
    private static byte[] encrypt(final byte[] data, final byte[] key, final String cipherAlgorithm) throws Exception {
        final Key k = toKey(key);
        return encrypt(data, k, cipherAlgorithm);
    }
    
    private static byte[] encrypt(final byte[] data, final Key key, final String cipherAlgorithm) throws Exception {
        final Cipher cipher = Cipher.getInstance(cipherAlgorithm);
        cipher.init(1, key);
        return cipher.doFinal(data);
    }
    
    public static byte[] decrypt(final String data, final String key) throws Exception {
        return decrypt(data.getBytes("UTF-8"), key.getBytes("UTF-8"), "AES/ECB/PKCS5Padding");
    }
    
    public static byte[] decrypt(final byte[] data, final byte[] key) throws Exception {
        return decrypt(data, key, "AES/ECB/PKCS5Padding");
    }
    
    private static byte[] decrypt(final byte[] data, final Key key) throws Exception {
        return decrypt(data, key, "AES/ECB/PKCS5Padding");
    }
    
    private static byte[] decrypt(final byte[] data, final byte[] key, final String cipherAlgorithm) throws Exception {
        final Key k = toKey(key);
        return decrypt(data, k, cipherAlgorithm);
    }
    
    private static byte[] decrypt(final byte[] data, final Key key, final String cipherAlgorithm) throws Exception {
        final Cipher cipher = Cipher.getInstance(cipherAlgorithm);
        cipher.init(2, key);
        return cipher.doFinal(data);
    }
    
    public static String showByteArray(final byte[] data) {
        if (null == data) {
            return null;
        }
        final StringBuilder sb = new StringBuilder("{");
        for (final byte b : data) {
            sb.append(b).append(",");
        }
        sb.deleteCharAt(sb.length() - 1);
        sb.append("}");
        return sb.toString();
    }
}
